import ConfigParser
import os
import sys


class Config:
    def __init__(self,file = '~/.backtwit'):
	self.file = file
	self.reload()

    def reload(self):
	self.debug = False
	self.show_time = 9       # Interval to show the message in seconds
	self.cycle_time = 60
	self.count = 5
	self.last_id = 0
	if self.file[0] == "~":
	    self.file = os.path.expanduser(self.file)
	    
	if os.path.exists(self.file):
	    config = ConfigParser.ConfigParser()
	    config.read(self.file)
	    self.debug = config.getboolean('Main', 'debug')
	    self.usr = config.get('Main', 'user')
	    self.password = config.get('Main', 'password')
	    self.show_time = config.getint('Main', 'show_time')
	    self.cycle_time = config.getint('Main', 'cycle_time')
	    self.count = config.getint('Main', 'count')
	    if (self.debug) : print "Config:reload:29: No Errors"
	else: 
	    print "Config:reload:31: Config not loaded! Using defaults."
	    self.usr = raw_input('Enter Twitter User:')
	    self.password = getpass.getpass('Enter Twitter Password:')
	    self.save()
	for arg in sys.argv:
	    if arg == "--debug":
		self.debug = True
	    if arg == "--nodebug":
		self.debug = False
	
    def save(self):
	""" 
	Function to store the configuration in a file
	"""
	config = ConfigParser.ConfigParser()
	config.add_section('Main')
	config.set('Main', 'debug', self.debug)
	config.set('Main', 'user', self.usr)
	config.set('Main', 'password', self.password)
	config.set('Main', 'show_time', self.show_time)
	config.set('Main', 'cycle_time', self.cycle_time)
	config.set('Main', 'count', self.cycle_time)
	config.write(open(self.file, 'wb'))